template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};


var loading = false; //状态标记
var hasNextPage = false;
var pageNo = 1;

queryList(pageNo);

//下拉刷新
$("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
    $("#weipai-list-content .left-list").html("");
    $("#weipai-list-content .right-list").html("");
    pageNo = 1;
    queryList(pageNo,function(){
        $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
    });
});

//上拉加载更多
$('#aroundBox').infinite().on("infinite", function () {
    if (hasNextPage){
        pageNo++;
        queryList(pageNo,function(){
        },true);
    }        
});

function queryList(pageNo,callback,isMore){
    if (loading) return;
    loading = true;
    var params = {};
    params.start = (pageNo - 1) * 20;
    params.limit = 20;
    /* global getCookie */
    /* global proType */
    params.token = getCookie("token");
    params.type = proType;//类型 1拍卖商品 2普通商品  状态 0仓库中 1出售中 2已成交
    params.state = "1,2";
    if (proType == 0) {
        delete params.type;
        params.state = 0;
    } 
        
    $.get('/antique/api/mallProduct/manage/query_list', params, function (result) {
        if (result.code == 0) {
            var leftList = [];
            var rightList = [];
            if(result.data.list){
                for(var i = 0;i<result.data.list.length;i++){
                    if(i%2 == 0){
                        leftList.push(result.data.list[i]);
                    } else {                        
                        rightList.push(result.data.list[i]);
                    }
                }
            }
            if (!isMore) {
                /* global template */
                document.getElementById('left-list-content').innerHTML = template('left-list', {list:leftList});
                document.getElementById('right-list-content').innerHTML = template('right-list', {list:rightList});
            } else {
                $('#left-list-content').append(template('left-list', {list:leftList}));
                $('#right-list-content').append(template('right-list', {list:rightList}));
            }
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;
        }

        
    });
}

function delPro (proId) {
    $.confirm("您是否确认删除？删除后将不可恢复", "", function() {
        $.post('/antique/api/mallProduct/manage/delete_product', { id:proId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                /**$("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true); */                
                window.location.reload();
            } else {
                $.toast(result.message, "cancel");
            }
        });
    }, function() {
    //取消操作
    });
    
}


function restorePro (proId) {
    $.confirm("您是否确认下架？下架后可在仓库查看", "", function() {
        $.post('/antique/api/mallProduct/manage/restore_product', { id:proId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                window.location.reload();
            } else {
                $.toast(result.message, "cancel");
            }
        });
    }, function() {
        //取消操作
    });
}

